package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.announcements;
import com.example.springboot.service.AmaMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/Announcements")
public class AmaMangerController {
    @Autowired
    private AmaMangerService amaMangerService;

    @PostMapping("/add")
    public Result addAnnouncement(@RequestBody announcements announcement) {
        System.out.println(announcement);
        amaMangerService.addAnnouncement(announcement);
        return Result.success("添加成功");
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<announcements> announcementList = amaMangerService.findAll();
        return Result.success(announcementList);
    }

    @PutMapping("/edit")
    public Result editAnnouncement(@RequestBody announcements  announcement) {
        amaMangerService.editAnnouncement(announcement);
        return Result.success("编辑成功");
    }

    @DeleteMapping("/delete/{announcement_id}")
    public String deleteAnnouncement(@PathVariable int announcement_id) {
        System.out.println(announcement_id);
        amaMangerService.deleteAnnouncement(announcement_id);
        return "Announcement deleted successfully!";
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteAnnouncementBatch(@RequestBody List<Long> ids) {
        try {
            amaMangerService.deleteAnnouncementBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/selectByTitle")
    public Result selectByTitle(@RequestParam String title) {
        return Result.success(amaMangerService.selectByTitle(title));
    }
}
